#include "SIX_CallBackBox.h"

SIX_CallBackBox::~SIX_CallBackBox()
{
}

SIX_CallBackBox *SIX_CallBackBox::Create(float x, float y, const ccColor4B& color, float width, float height, float shakeOffset, MOVABLE_TYPE showType, MOVABLE_TYPE hideType, bool bSeqExec, bool bCloseThenCleanup)
{
	CCScene *pScene = CCDirector::sharedDirector()->getRunningScene();
	if (!pScene)
		return 0;

	SIX_CallBackBox *pPopWnd = new SIX_CallBackBox();
	if (pPopWnd && pPopWnd->init())
	{
		pPopWnd->initWithColor(color, width, height);
		pPopWnd->setPosition(x, y);
		pPopWnd->setRedrawBorder(false);
		pPopWnd->m_ForbiddenTouch = true;
		pPopWnd->m_bCloseThenCleanup = bCloseThenCleanup;

		pPopWnd->setShakeOffset(shakeOffset);
		pPopWnd->setShowType(showType);
		pPopWnd->setHideType(hideType);
		pPopWnd->setSeqExec(bSeqExec);
	}
	return pPopWnd;
}

bool SIX_CallBackBox::init()
{
	if (!SIX_EffectPopWnd::init())
		return false;
	pText = 0;
	pTarget = 0;
	for (int i=CALLBACK_EVENT_SUBMIT;i<=CALLBACK_EVENT_DELAY;i++)
		pfnCallback[i] = 0;
	return true;
}

void SIX_CallBackBox::onEnterTransitionDidFinish()
{
	SIX_PopWnd::onEnterTransitionDidFinish();
}

void SIX_CallBackBox::setTextAttribute(float x,float y,float w,float h,const char *fontName,int fontSize,CCTextAlignment hAlignment,CCVerticalTextAlignment hVerAlignment,CCPoint ap)
{
	if (!fontName || !strlen(fontName))
		return;

	if (fontSize<1)
		fontSize = 12;

	if (!pText)
	{
		pText = CCLabelTTF::create();
		if (!pText)
			return;
		this->addChild(pText);
	}

	if (w==0.0f)
		w = this->getContentSize().width;
	if (h==0.0f)
		h = this->getContentSize().height;

#if (CC_TARGET_PLATFORM!=CC_PLATFORM_IOS)
	pText->setFontName(fontName);
#endif
	pText->setAnchorPoint(ap);
	pText->setFontSize((float)fontSize);
	pText->setDimensions(CCSize(w,h));
	pText->setHorizontalAlignment(hAlignment);
	pText->setVerticalAlignment(hVerAlignment);
	pText->setPosition(ccp(x,y));
}

void SIX_CallBackBox::setString(const char *text)
{
	if (!text || !strlen(text) || !pText)
		return;

	pText->setVisible(true);
	pText->setString(text);
	SIX_Utility::GetInstance()->FixedTextTexParameters(pText->getTexture());
}

void SIX_CallBackBox::setCallbackWithEvent(CCNode *target,SEL_CallFuncN pfn,CALLBACK_EVENT_TYPE type,float delay)
{
	if (type<CALLBACK_EVENT_SUBMIT || type>CALLBACK_EVENT_DELAY)
		return;

	pTarget = target;
	pfnCallback[type] = pfn;

	if (type==CALLBACK_EVENT_DELAY && delay>0.0f)
	{
		CCDelayTime *pDelay = CCDelayTime::create(delay);
		CCCallFunc *pCallfun = CCCallFunc::create(this,callfunc_selector(SIX_CallBackBox::OnCallbackWithDelay));
		CCFiniteTimeAction *pSequence = CCSequence::create(pDelay,pCallfun,0);
		this->runAction(pSequence);
	}
}

void SIX_CallBackBox::OnCallbackWithDelay()
{
	if (!pTarget || (pTarget  && !pfnCallback[CALLBACK_EVENT_DELAY]))
		return;

	(pTarget->*pfnCallback[CALLBACK_EVENT_DELAY])(this);
}

bool SIX_CallBackBox::OnSubmit()
{
	if (!pTarget || (pTarget  && !pfnCallback[CALLBACK_EVENT_SUBMIT]))
		return true;

	(pTarget->*pfnCallback[CALLBACK_EVENT_SUBMIT])(this);
	//CCNotificationCenter::sharedNotificationCenter()->postNotification(NOTIFY_CLOSEWND_CLICKED);

	return true;
}

bool SIX_CallBackBox::OnClose()
{
	if (!pTarget || (pTarget  && !pfnCallback[CALLBACK_EVENT_CLOSE]))
		return true;

	(pTarget->*pfnCallback[CALLBACK_EVENT_CLOSE])(this);

	return true;
}

bool SIX_CallBackBox::OnCancel()
{
	if (!pTarget || (pTarget  && !pfnCallback[CALLBACK_EVENT_CANCEL]))
		return true;

	(pTarget->*pfnCallback[CALLBACK_EVENT_CANCEL])(this);

	return true;
}